package sort;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Scanner;

public class Code1_SelectionSort {

    static class User {
        String name;
        int socre;

        public User(String name, int socre) {
            this.name = name;
            this.socre = socre;
        }

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public int getSocre() {
            return socre;
        }

        public void setSocre(int socre) {
            this.socre = socre;
        }

        @Override
        public String toString() {
            return "User{" +
                    "name='" + name + '\'' +
                    ", socre=" + socre +
                    '}';
        }
    }
    public static void main(String[] args) throws IOException {
        int num = 0;   //用户数量
        int desc;
        BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
        while (br.ready()) {
            num = Integer.parseInt(br.readLine());
            desc = Integer.parseInt(br.readLine());
            User[] user = new User[num];
            for (int i = 0; i < num; i++) {
                String[] str = br.readLine().split(" ");
                user[i] = new User(str[0], Integer.parseInt(str[1]));
            }

            if (desc == 1) {
                for (User u : sortByScoreAsc(user)) {
                    System.out.println(u.name + " " + u.socre);
                }
            } else if (desc == 0) {
                for (User u : sortByScoreDesc(user)) {
                    System.out.println(u.name + " " + u.socre);
                }
            }
        }
    }

    /*
    * Asc为升序  这里排序方法选择插入排序
    * */
    public static User[] sortByScoreAsc(User[] user){
        for (int a=0;a<user.length;a++){
         for (int b=a;b>0 && user[b].socre < user[b-1].socre;b--){
                 swap(user,b,b-1);
         }
        }
        return user;
    }

    public static User[] sortByScoreDesc(User[] u){

        for (int a=0;a<u.length;a++){
            for (int b=a;b>0 && u[b].socre > u[b-1].socre;b--){
                swap(u,b,b-1);
            }
        }
        return u;
    }

    public static void swap(User[] u,int a,int b){
        User tempU;
        tempU=u[a];
        u[a]=u[b];
        u[b]=tempU;
    }



    /*
    * 二分查找指定值  如果不存在返回-1  存在返回0
    * */
    public static int binSearch(int[] arr,int n){
        int begin=0;
        int end=arr.length;
        int mid;
        while (end>begin){
            mid=(begin+end)/2;
            if(arr[mid] == n){
                return mid;
            }else if (arr[mid] > n){
                end=mid-1;
            }else if (arr[mid] < n){
                begin=mid+1;
            }
        }

        return -1;
    }

    /*
    * 实现插入排序
    * */
    public static void insertSort(int[] arr){
        for (int a=0;a<arr.length-1;a++){
            for (int b=a+1;b>0 && arr[b] < arr[b-1];b--){
                    swap(arr,b,b-1);
            }
        }
    }


    /*
    * 实现选择排序
    * */
    public static void selectSort(int[] arr){
        int minIndex=0;
        for (int a=0;a<arr.length;a++){
            minIndex=a;
            for (int b=a+1;b<arr.length;b++){
            minIndex=arr[b]<arr[minIndex]?b:minIndex;
            }
        swap(arr,a,minIndex);
        }
    }

    /*
    * 执行交换
    * */
    public static void swap(int[] arr,int a,int b){
        int temp=arr[a];
        arr[a]=arr[b];
        arr[b]=temp;

    }
}
